﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            string password = TextBox2.Text;
            string email = TextBox3.Text;
            string gender = RadioButton1.Checked ? "1" : "0";

            string sql = "insert into StudentInfo (stu_name , gender , email , [password]) values (@name , @gender , @email , @pwd)";
            SqlParameter[] pras =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@pwd",password),
                new SqlParameter("@email",email),
                new SqlParameter("@gender",gender)
            };
            string constr = "server = . ; uid = sa ; password = 123456 ; database = Student_db";
            SqlConnection con = new SqlConnection(constr);
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(pras);
                int result = cmd.ExecuteNonQuery();
                if (result != -1)
                {
                    Literal1.Text = "注册成功！<a href = 'Login.aspx'>去登录</a>";
                }
                else
                {
                    Literal1.Text = "注册失败！";
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally 
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}